/**
 * Created by tsy on 2016/6/14.
 */
$(function () {
    $('#month_time').datebox({
        onShowPanel: function () {//显示日趋选择对象后再触发弹出月份层的事件，初始化时没有生成月份层
            span.trigger('click'); //触发click事件弹出月份层
            if (!tds) setTimeout(function () {//延时触发获取月份对象，因为上面的事件触发和对象生成有时间间隔
                tds = p.find('div.calendar-menu-month-inner td');
                tds.click(function (e) {
                    e.stopPropagation(); //禁止冒泡执行easyui给月份绑定的事件
                    var year = /\d{4}/.exec(span.html())[0]//得到年份
                        , month = parseInt($(this).attr('abbr'), 10); //月份
                    $('#month_time').datebox('hidePanel')//隐藏日期对象
                        .datebox('setValue', year + '-' + month); //设置日期的值
                });
            }, 0)
        },
        parser: function (s) {//配置parser，返回选择的日期
            if (!s) return new Date();
            var arr = s.split('-');
            return new Date(parseInt(arr[0], 10), parseInt(arr[1], 10) - 1, 1);
        },
        formatter: function (d) { return d.getFullYear() + '-' + (d.getMonth()+1); }//配置formatter，只返回年月
    });
    var p = $('#month_time').datebox('panel'), //日期选择对象
        tds = false, //日期选择对象中月份
        span = p.find('span.calendar-text'); //显示月份层的触发控件
});

function submitSearch(){
    $('#report_table').datagrid('load',{
        month_time: $('#month_time').datebox("getValue"),
        select_name:$('#select_name').val()
    });
}

function resetSearch(){
    $("#start_time").textbox("setValue","");
    $("#end_time").textbox("setValue","");
    $("#select_name").textbox("setValue","");
}

function exportReport(){
    var url = "../../Report/loaninforeport/loaninfo_report_export";
    $.ajax({
        type: "post",
        async: true,
        data: $("#searchTable").serialize(),
        url: url,
        success: function(d){
            console.log(d);
            //var file="http://120.24.52.49:8010/tp_bypal/Public/file/"+d;
            var file="http://test.bypal.com.cn/Public/file/"+d;
            //var file="http://localhost/tp_bypal/Public/file/"+d;
            Downer(file);
        },
        error: function(e){
            alert(e);
        }
    });
}
var editIndex = undefined;
function endEditing(){
    if (editIndex == undefined){return true}
    if ($('#report_table').datagrid('validateRow', editIndex)){
        //var ed = $('#report_table').datagrid('getEditor', {index:editIndex,field:'borrow_id'});
        //var productname = $(ed.target).combobox('getText');
        //$('#report_table').datagrid('getRows')[editIndex]['productname'] = productname;
        $('#report_table').datagrid('endEdit', editIndex);
        editIndex = undefined;
        return true;
    } else {
        return false;
    }
}
function onClickRow(index){
    if (editIndex != index){
        if (endEditing()){
            $('#report_table').datagrid('selectRow', index)
                .datagrid('beginEdit', index);
            editIndex = index;
        } else {
            $('#report_table').datagrid('selectRow', editIndex);
        }
    }
}

function update_contract(){
    if (endEditing()){
        var obj=$("#report_table").datagrid('getChanges');
        console.log(obj);
        var data={"data":obj};
        var url="../../Report/applyreport/update_contract";
        $.ajax({
            type: "post",
            async: true,
            data: data,
            url: url,
            success: function(d){
                console.log(d);
                if(d!=0){
                    alert("修改"+d+"条数据成功");
                    $('#report_table').datagrid('acceptChanges');
                }else{
                    alert("操作失败");
                }
            },
            error: function(e){
                alert(e);
            }
        });
    }
}